import 'dart:convert';

import 'package:xindong/models/xindong_yinsi.dart';
import 'package:xindong/models/xindong_location_class.dart';
import 'package:shared_preferences/shared_preferences.dart';

class XinDongStorageKey {
  static const String account = "sp_account";
  static const String phone = "sp_phone";
  static const String agreement = "sp_agreement";
  static const String agreementHuiYuan = "sp_agreementHuiYuan";
  static const String invite = "sp_invite";
  static const String location = "sp_location";
  static const String loginId = "sp_loginId";
  static const String loginSig = "sp_loginSig";
  static const String sex = "sp_sex";
  static const String kefu = "sp_kefu";
}

class XinDongStorage {
  late SharedPreferences prefs;

  /// 初始化全局信息，会在APP启动时执行
  Future setup() async {
    prefs = await SharedPreferences.getInstance();
    return Future.value();
  }

  /// ======== 单例
  // 工厂方法构造函数
  factory XinDongStorage() => _getInstance();

  // instance的getter方法，singletonManager.instance获取对象
  static XinDongStorage get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static XinDongStorage? _instance;

  // 获取对象
  static XinDongStorage _getInstance() {
    _instance ??= XinDongStorage._internal();
    return _instance!;
  }

  // 私有的自定义命名式构造方法，通过它实现一个类;
  // 子类不能继承internal
  // 不是关键字，可定义其他名字
  XinDongStorage._internal() {
    //初始化(设置一些默认的)...
  }

  /// ======== 第三方 ========

  // Object.
  Future<bool>? saveObject(String key, Object value) {
    return prefs.setString(key, json.encode(value));
  }

  Map? getObject(String key) {
    String? jsonString = prefs.getString(key);
    return (jsonString == null || jsonString.isEmpty) ? null : json.decode(jsonString);
  }

  T? getFormatObject<T>(String key, T Function(Map v) func, {T? defaultValue}) {
    Map? map = getObject(key);
    return map == null ? defaultValue : func(map);
  }

  // StringList
  Future<bool>? saveStringList(String key, List<String> value) {
    return prefs.setStringList(key, value);
  }

  List<String>? getStringList(String key) {
    return prefs.getStringList(key);
  }

  // Bool
  Future<bool>? saveBool(String key, bool value) {
    return prefs.setBool(key, value);
  }

  bool? getBool(String key, {bool? defValue = false}) {
    return prefs.getBool(key) ?? defValue;
  }

  // String
  Future<bool>? saveString(String key, String value) {
    return prefs.setString(key, value);
  }

  String? getString(String key) {
    return prefs.getString(key);
  }

  // Int
  Future<bool>? saveInt(String key, int value) {
    return prefs.setInt(key, value);
  }

  int? getInt(String key) {
    return prefs.getInt(key);
  }

  // remove
  Future<bool>? remove(String key) {
    return prefs.remove(key);
  }
}

/// ======== 用户
extension XinDongAccountStorage on XinDongStorage {
  // Future<bool>? saveAccount(XinDongAccount account) {
  //   return saveObject(XinDongStorageKey.account, account);
  // }
  //
  // XinDongAccount? getAccount() {
  //   try {
  //     return getFormatObject(XinDongStorageKey.account, (v) => XinDongAccount.fromJson(Map<String, dynamic>.from(v)));
  //   } catch (e) {
  //     // 用户数据不匹配时，抛出异常，重新登录
  //     return null;
  //   }
  // }
  //
  // Future<bool>? removeAccount() {
  //   return remove(XinDongStorageKey.account);
  // }
}

/// ======== 协议
extension XinDongAgreementStorage on XinDongStorage {
  Future<bool>? saveAgreement(XinDongYinSi model) {
    return saveObject(XinDongStorageKey.agreement, model);
  }

  getAgreement() {
    try {
      return getFormatObject(
          XinDongStorageKey.agreement, (v) => XinDongYinSi.fromJson(Map<String, dynamic>.from(v)));
    } catch (e) {
      return null;
    }
  }
}

// /// ======== iOS内购买
// extension XinDongPurchaseStorage on XinDongStorage {
//   Future<bool>? savePurchase(XinDongIAPClass model) {
//     return SpUtil.putObject(SP_PURCHASE, model);
//   }
//
//   Future<bool>? deletePurchase() {
//     return SpUtil.remove(SP_PURCHASE);
//   }
//
//   getPurchase() {
//     return SpUtil.getObj(SP_PURCHASE, (v) => XinDongIAPClass.fromJson(Map<String, dynamic>.from(v)));
//   }
// }

/// ======== 邀请
// extension XinDongInviteStorage on XinDongStorage {
//   Future<bool>? saveInvite(XinDongInviteClass model) {
//     return saveObject(XinDongStorageKey.invite, model);
//   }
//
//   getInvite() {
//     return getFormatObject(XinDongStorageKey.invite, (v) => XinDongInviteClass.fromJson(Map<String, dynamic>.from(v)));
//   }
// }

/// ======== 位置
extension XinDongLocationStorage on XinDongStorage {
  Future<bool>? saveLocation(XinDongLocationClass model) {
    return saveObject(XinDongStorageKey.location, model);
  }

  getLocation() {
    try {
      return getFormatObject(
          XinDongStorageKey.location, (v) => XinDongLocationClass.fromJson(Map<String, dynamic>.from(v)));
    } catch (e) {
      return null;
    }
  }
}
